﻿using System;
using System.Collections.Generic;
using System.Web.Services;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;

/// <summary>
/// A class to provide the auto complete feature.
/// </summary>
/// The following was created by mgsurya developed on the machineMGSURYA-PC on 12/4/2012, 10:23 AM
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]

public class AutoComplete : WebService
{
    public AutoComplete()
    {
        // Constructor
    }

    /// <summary>
    /// Gets the completion list.
    /// </summary>
    /// <param name="prefixText">The prefix text.</param>
    /// <param name="count">The count.</param>
    /// <returns></returns>
    /// The following was created by mgsurya developed on the machineMGSURYA-PC on 12/4/2012, 10:24 AM
    [WebMethod]
    public string[] GetCompletionList(string prefixText, int count)
    {
        List<string> listString = new List<string>();
        string connectionString = ConfigurationManager.AppSettings["SQLSERVER_CONNECTION_STRING"];
        SqlCommand myCommand = new SqlCommand();
        DataTable dataTable = new DataTable();
        SqlConnection myConnection = new SqlConnection(connectionString);
        String[] array = { };
        string query = "SELECT url_descriptor" +
                        " FROM url_info " + 
                        " WHERE url_descriptor like '"
                        + prefixText + "%' GROUP BY url_descriptor ORDER BY url_descriptor ASC";
        myCommand.CommandText = query;
        myCommand.Connection = myConnection;
        try
        {
            myConnection.Open();
            SqlDataAdapter ad = new SqlDataAdapter(myCommand);
            ad.Fill(dataTable);

            array = Array.ConvertAll<DataRow, String>(dataTable.Select(), delegate(DataRow row)
                             { return (String)row["url_descriptor"]; });

            myCommand.ExecuteNonQuery();
            myCommand.Connection.Close();
        }
        finally { }
        return array;
    }
}
